<script type="text/html" template>
  <link rel="stylesheet" href="{{ layui.setter.base }}style/login.css?v={{ layui.admin.v }}-1" media="all">
</script>


<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

  <div class="layadmin-user-login-main">
    <div class="layadmin-user-login-box layadmin-user-login-header">
      <h2>LQBCMS6</h2>
      <p>技术支持：https://www.liqingbo.cn/</p>
    </div>
    <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="LAY-user-login-username"></label>
        <input type="text" name="username" id="LAY-user-login-username" lay-verify="required" placeholder="用户名"
          class="layui-input">
      </div>
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
        <input type="password" name="password" id="LAY-user-login-password" lay-verify="required" placeholder="密码"
          class="layui-input">
      </div>
      <div class="layui-form-item">
        <div class="layui-row">
          <div class="layui-col-xs7">
            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label>
            <input type="text" name="vercode" id="LAY-user-login-vercode" lay-verify="required" placeholder="图形验证码"
              class="layui-input">
          </div>
          <div class="layui-col-xs5">
            <div style="margin-left: 10px;">
              <img src="/api/login/verify" alt="captcha" class="layadmin-user-login-codeimg" id="LAY-user-get-vercode">
            </div>
          </div>
        </div>
      </div>

      <div class="layui-form-item">
        <button class="layui-btn layui-btn-fluid" id="subBtn" lay-submit lay-filter="LAY-user-login-submit">登 入</button>
      </div>
      <div class="layui-trans layui-form-item layadmin-user-login-other">
      </div>
    </div>
  </div>

</div>

<script>
  layui.use(['admin', 'form', 'user'], function () {
    var $ = layui.$
      , setter = layui.setter
      , admin = layui.admin
      , form = layui.form
      , router = layui.router()
      , search = router.search;


    document.onkeyup = function (e) {      //onkeyup是javascript的一个事件、当按下某个键弹起 var _key;                                                 //的时触发
      if (e == null) { // ie
        _key = event.keyCode;
      } else { // firefox              //获取你按下键的keyCode
        _key = e.which;          //每个键的keyCode是不一样的
      }

      if (_key == 13) {   //判断keyCode是否是13，也就是回车键(回车的keyCode是13)
        //if (validator(document.loginform)){ //这个因该是调用了一个验证函数
        document.getElementById('subBtn').click()    //验证成功触发一个Id为btnLogin的
        //}                                                                        //按钮的click事件，达到提交表单的目的
      }
    }


    form.render();

    //提交
    form.on('submit(LAY-user-login-submit)', function (obj) {

      //请求登入接口
      admin.req({
        type: "POST",
        url: '/api/login/login' //实际使用请改成服务端真实接口
        , data: obj.field
        , success: function (res) {
          console.log(111);
          console.log(res.msg);

          if (res.code == 0) {
            //请求成功后，写入 access_token
            layui.data(setter.tableName, {
              key: setter.request.tokenName
              , value: res.data.access_token
            });

            layui.data('lang', {
              key: 'lang',
              value: 'zh'
            });

            //登入成功的提示与跳转
            layer.msg('登入成功', {
              offset: '15px'
              , icon: 1
              , time: 1000
            }, function () {
              location.hash = search.redirect ? decodeURIComponent(search.redirect) : '/';
            });
          } else {
            var othis = $("#LAY-user-get-vercode");
            var src = '/api/login/verify?t=' + new Date().getTime()
            othis.attr('src', src);

            layer.msg(res.msg);

          }

        }
      });

    });


    //实际使用时记得删除该代码
    /*layer.msg('为了方便演示，用户名密码可随意输入', {
      offset: '15px'
      ,icon: 1
    });*/

  });
</script>